package org.ryaness.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.util.Arrays;

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {

    private final String WORK_DIR = "D:/test_dir";

    private final String SOURCE_DIR =  WORK_DIR + "/source";

    private final String OPT_RAW_DIR = WORK_DIR + "/opt/raw";

    private final String OPT_DEALED_DIR = WORK_DIR + "/opt/dealed";

    private final String DEST_DIR = WORK_DIR + "/dest";

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("open");
        // 获取源文件
//        File[] sources = FileUtil.ls(SOURCE_DIR);
//
//        // 解压xxx.zip
//        Arrays.stream(sources)
//                .flatMap(file->{
//
//                    File rootFolder = ZipUtil.unzip(file, new File(OPT_RAW_DIR), Charset.forName("GBK"));
//
//                    // 学季目录
//                    File[] yearQuarterFolder = FileUtil.ls(rootFolder.getAbsolutePath());
//
//                    return Arrays.stream(yearQuarterFolder).flatMap(yqFolder->{
//                        // 代码目录
//                        File[] subscripCodeFolders = FileUtil.ls(yqFolder.getAbsolutePath());
//
//                        String year = yqFolder.getName().substring(0, 4);
//                        String quarterName = yqFolder.getName().substring(4);
//
//                        return Arrays.stream(subscripCodeFolders).map(subscripCodeFolder->{
//
//                            String subscripCode = subscripCodeFolder.getName();
//
//                            File[] files = FileUtil.ls(subscripCodeFolder.getAbsolutePath());
//
//                            return new FolderInfo().setYear(year).setQuarterName(quarterName).setSubscripCode(subscripCode).setFiles(files);
//                        });
//                    });
//                })
//                .forEach(folderInfo -> {
//                    try {
//                        // 处理封面01
//                        File file01 = folderInfo.getFiles()[0];
//                        detailPicture(file01, folderInfo.year, folderInfo.getQuarterName(), folderInfo.subscripCode);
//
//                        // 处理封面02
//                        File file02 = folderInfo.getFiles()[1];
//                        detailPicture(file02, folderInfo.year, folderInfo.getQuarterName(), folderInfo.subscripCode);
//
//                        // 处理详情图
//                        File file03 = folderInfo.getFiles()[2];
//                        coverPicture(file03, folderInfo.year, folderInfo.getQuarterName(), folderInfo.subscripCode);
//
//
//                    }catch (Exception e){
//                        e.printStackTrace();
//                    }
//                });

        // 拷贝文件至中间目录

        // 裁切、缩放图片

        // 拷贝文件至目标目录

    }

//    private void coverPicture(File pictureFile, String year, String quarterName, String subscripCode) throws Exception {
//        // 拓展名
//        String extName = FileUtil.extName(pictureFile);
//
//        BufferedImage image = ImageIO.read(pictureFile);
//        // 缩放至宽度1080
//        BufferedImage newImage = new BufferedImage(1080, 1080 * image.getHeight() / image.getWidth(), BufferedImage.TYPE_INT_RGB);
//        newImage.getGraphics().drawImage(image.getScaledInstance(1080, 1080 * image.getHeight() / image.getWidth(), Image.SCALE_SMOOTH), 0, 0, null);
//
//        // 按最大2000的高度进行裁剪
//        int height = newImage.getHeight();
//        int offset = 2000;
//        int index = 0;
//        do {
//            int subHeight = index + offset < height ? offset : height - index;
//            BufferedImage subImage = newImage.getSubimage(0, 0, newImage.getWidth(), subHeight);
//            File dealedFile = FileUtil.touch(new File(OPT_DEALED_DIR + "/" + year + quarterName + "/" + subscripCode + "/" + index + "/" +pictureFile.getName()));
//            ImageIO.write(subImage,extName, dealedFile);
//            index += subHeight;
//        }while(index + offset <= height);
//
//
//    }
//
//    /**
//     * 详情图处理
//     */
//    private void detailPicture(File detailPicture, String year, String quarterName, String subscripCode) throws Exception {
//
//        // 拓展名
//        String extName = FileUtil.extName(detailPicture);
//
//        // 处理封面01
//        BufferedImage image = ImageIO.read(detailPicture);
//
//        int min = Math.min(image.getHeight(), image.getWidth());
//        image = image.getSubimage(0,0, min, min);
//
//        System.out.printf("width:%s, height:%s%n", image.getWidth(), image.getHeight());
//
//        BufferedImage newImage = new BufferedImage(1080, 1080, BufferedImage.TYPE_INT_RGB);
//        newImage.getGraphics().drawImage(image.getScaledInstance(1080, 1080, Image.SCALE_SMOOTH),0, 0, null);
//
//        File dealedFile = FileUtil.touch(new File(OPT_DEALED_DIR + "/" + year + quarterName + "/" + subscripCode + "/" + detailPicture.getName()));
//
//        ImageIO.write(newImage,extName, dealedFile);
//    }

    @Data
    @Accessors(chain = true)
    public static class FolderInfo {

        private String year;

        private String quarterName;

        private String subscripCode;

        private File[] files;

    }

}
